{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Goal Parser Interpretation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: interpret"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #设计 Goal 解析器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>请你设计一个可以解释字符串 <code>command</code> 的 <strong>Goal 解析器</strong> 。<code>command</code> 由 <code>\"G\"</code>、<code>\"()\"</code> 和/或 <code>\"(al)\"</code> 按某种顺序组成。Goal 解析器会将 <code>\"G\"</code> 解释为字符串 <code>\"G\"</code>、<code>\"()\"</code> 解释为字符串 <code>\"o\"</code> ，<code>\"(al)\"</code> 解释为字符串 <code>\"al\"</code> 。然后，按原顺序将经解释得到的字符串连接成一个字符串。</p>\n",
    "\n",
    "<p>给你字符串 <code>command</code> ，返回<em> </em><strong>Goal<em><strong> </strong></em>解析器 </strong>对<em> </em><code>command</code> 的解释结果。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>command = \"G()(al)\"\n",
    "<strong>输出：</strong>\"Goal\"\n",
    "<strong>解释：</strong>Goal 解析器解释命令的步骤如下所示：\n",
    "G -&gt; G\n",
    "() -&gt; o\n",
    "(al) -&gt; al\n",
    "最后连接得到的结果是 \"Goal\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>command = \"G()()()()(al)\"\n",
    "<strong>输出：</strong>\"Gooooal\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>command = \"(al)G(al)()()G\"\n",
    "<strong>输出：</strong>\"alGalooG\"\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= command.length &lt;= 100</code></li>\n",
    "\t<li><code>command</code> 由 <code>\"G\"</code>、<code>\"()\"</code> 和/或 <code>\"(al)\"</code> 按某种顺序组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [goal-parser-interpretation](https://leetcode.cn/problems/goal-parser-interpretation/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [goal-parser-interpretation](https://leetcode.cn/problems/goal-parser-interpretation/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"G()(al)\"', '\"G()()()()(al)\"', '\"(al)G(al)()()G\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "            return command.replace('()','o').replace('(al)','al')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        s=command.replace('()','o') \n",
    "        s1=s.replace('(al)','al')\n",
    "        return s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command=command.replace('(al)','al')\n",
    "        command=command.replace('()','o')\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command = command.replace(\"()\", \"o\")\n",
    "        command = command.replace('(al)', 'al')\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command=command.replace('()','o')\n",
    "        command=command.replace('(al)','al')\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\",'o').replace('(al)','al')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        dic = {\n",
    "            '()':'o',\n",
    "            'G':'G',\n",
    "            '(al)':'al'\n",
    "        }\n",
    "        ans = ''\n",
    "        i = 0\n",
    "        while i < len(command):\n",
    "            if command[i] == 'G':\n",
    "                ans += dic['G']\n",
    "                i += 1\n",
    "            elif command[i:i+2] == '()':\n",
    "                ans += dic['()']\n",
    "                i += 2\n",
    "            else:\n",
    "                ans += dic['(al)']\n",
    "                i += 4\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = []\n",
    "        for i, c in enumerate(command):\n",
    "            if c == 'G':\n",
    "                res.append(c)\n",
    "            elif c == '(':\n",
    "                res.append('o' if command[i+1] == ')'  else 'al')\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        n = len(command)\n",
    "        ans = []\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if command[i] == '(' and command[i+1] == ')':\n",
    "                ans.append('o')\n",
    "                i = i + 2\n",
    "            elif command[i] == '(' and command[i+1] == 'a':\n",
    "                ans.append('al')\n",
    "                i = i + 4\n",
    "            else:\n",
    "                ans.append('G')\n",
    "                i += 1\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "\n",
    "        res = ''\n",
    "        idx = 0\n",
    "\n",
    "        while idx < len(command):\n",
    "            \n",
    "            if command[idx] == 'G':\n",
    "                res += 'G'\n",
    "                idx += 1\n",
    "            \n",
    "            else:\n",
    "                if command[idx+1] == ')':\n",
    "                    res += 'o'\n",
    "                    idx += 2\n",
    "                else:\n",
    "                    res += 'al'\n",
    "                    idx += 4\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\",\"o\").replace(\"(al)\",\"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        i = 0\n",
    "        res = ''\n",
    "        while i < len(command):\n",
    "            if command[i] == 'G':\n",
    "                res += 'G'\n",
    "                i += 1\n",
    "            elif command[i] == '(':\n",
    "                if i + 1 < len(command):\n",
    "                    if command[i + 1] == ')':\n",
    "                        res += 'o'\n",
    "                        i += 2\n",
    "                    elif command[i + 1] == 'a':\n",
    "                        res += 'al'\n",
    "                        i += 4\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = \"\"\n",
    "        temp = \"\"\n",
    "        for c in command: \n",
    "            temp+= c \n",
    "            if temp == \"G\":\n",
    "                res += \"G\"\n",
    "                temp = \"\"\n",
    "            elif temp ==\"()\":\n",
    "                res += \"o\"\n",
    "                temp = \"\"\n",
    "            elif temp ==\"(al)\":\n",
    "                res += \"al\"\n",
    "                temp = \"\"\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = ''\n",
    "\n",
    "        for i, c in enumerate(command):\n",
    "            if c == \"G\":\n",
    "                res += 'G'\n",
    "                continue\n",
    "            \n",
    "            if c == \")\":\n",
    "                if command[i-1] == \"(\":\n",
    "                    res += 'o'\n",
    "                    continue\n",
    "                res += 'al'\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\", \"o\").replace(\"(al)\", \"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        # return command.replace(\"()\",\"o\").replace(\"(al)\",\"al\")\n",
    "        res = \"\"\n",
    "        for i, ch in enumerate(command):\n",
    "            if ch == \"G\":\n",
    "                res += \"G\"\n",
    "            elif ch == \"(\":\n",
    "                res += \"o\" if command[i + 1] == \")\" else \"al\"\n",
    "        return res\n",
    "        \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = \"\"\n",
    "        for i in range(len(command)):\n",
    "            if command[i] == \"G\":\n",
    "                res += \"G\"\n",
    "            elif command[i] == \")\" and command[i-1] == \"(\":\n",
    "                res += \"o\"\n",
    "            elif command[i] == \"a\":\n",
    "                res += \"al\"\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace('()','o').replace('(al)','al')\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command = command.replace('()','o')\n",
    "        command = command.replace('(al)','al')\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        stack = ''\n",
    "        ans = ''\n",
    "        for i in command:\n",
    "            stack += i\n",
    "            if stack == '()':\n",
    "                ans += 'o'\n",
    "                stack = ''\n",
    "            elif stack == 'G':\n",
    "                ans += 'G'\n",
    "                stack = ''\n",
    "            elif stack == '(al)':\n",
    "                ans += 'al'\n",
    "                stack = ''\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = []\n",
    "        for i,c in enumerate(command):\n",
    "            if c=='G':\n",
    "                res.append(c)\n",
    "            elif c=='(':\n",
    "                res.append('o' if command[i+1]==')' else 'al')\n",
    "        return ''.join(res)\n",
    "'''\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace('(al)','al').replace('()','o')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = ''\n",
    "\n",
    "        index = 0\n",
    "        length = len(command)\n",
    "        while index < length:\n",
    "            if command[index] == 'G':\n",
    "                res += 'G'\n",
    "                index += 1\n",
    "                continue\n",
    "\n",
    "            if command[index] == '(':\n",
    "                if command[index+1] == ')':\n",
    "                    res += 'o'\n",
    "                    index += 2 \n",
    "                else:\n",
    "                    res += 'al'\n",
    "                    index += 4\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        item = command.replace(\"()\",\"o\").replace(\"(al)\",\"al\")\n",
    "        return item\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command = command.replace(\"()\", \"o\")\n",
    "        command = command.replace('(al)', 'al')\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        # 思路：哈希表保存三个字符串及其对应的解析字符串\n",
    "\n",
    "        paser_dict = {\"G\": \"G\", \"()\": \"o\", \"(al)\": \"al\"}\n",
    "        ans = \"\"\n",
    "        temp = \"\"\n",
    "        for ch in command:\n",
    "            temp += ch\n",
    "            if temp in paser_dict:\n",
    "                ans += paser_dict[temp]\n",
    "                temp = \"\"\n",
    "        return ans\n",
    "\n",
    "   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command = command.replace(\"()\", \"o\")\n",
    "        command = command.replace('(al)', 'al')\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        newStr = command.replace(\"(al)\",\"al\")\n",
    "        newStr = newStr.replace(\"()\",\"o\")\n",
    "        return newStr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        length=len(command)\n",
    "        res=[]\n",
    "        for i in range(length):\n",
    "            if command[i]=='G':\n",
    "                res.append('G')\n",
    "            if command[i]=='(':\n",
    "                if command[i+1]==')':\n",
    "                    res.append('o')\n",
    "                    i+=1\n",
    "                else:\n",
    "                    res.append('al')\n",
    "                    i+=3\n",
    "        return ''.join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\", \"o\").replace(\"(al)\", \"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        \n",
    "        return command.replace(\"()\",\"o\").replace(\"(al)\",\"al\")\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\",\"o\").replace(\"(al)\",\"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, s: str) -> str:\n",
    "        ret = ''\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if s[i] == \"G\":\n",
    "                ret += 'G'\n",
    "                i += 1\n",
    "            else:\n",
    "                if s[i + 1] == ')':\n",
    "                    i += 2\n",
    "                    ret += 'o'\n",
    "                else:\n",
    "                    ret += 'al'\n",
    "                    i += 4\n",
    "        return ret\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        i = 0\n",
    "        res = \"\"\n",
    "        while i<len(command):\n",
    "            if command[i]==\"G\":\n",
    "                res = res+\"G\"\n",
    "                i = i+1\n",
    "            elif command[i]==\"(\":\n",
    "                if command[i+1]==\")\":\n",
    "                    res = res+\"o\"\n",
    "                    i = i+2\n",
    "                elif command[i+1]==\"a\":\n",
    "                    res = res+\"al\"\n",
    "                    i = i+4\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = []\n",
    "        for i, c in enumerate(command):\n",
    "            if c == 'G':\n",
    "                res.append(c)\n",
    "            elif c == '(':\n",
    "                res.append('o' if command[i+1]==')' else 'al')\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        n = len(command)\n",
    "        ans = []\n",
    "        idx = 0\n",
    "        while idx < n:\n",
    "            if command[idx] == 'G':\n",
    "                ans.append('G')\n",
    "                idx += 1\n",
    "            elif idx+1 < n and command[idx+1] == ')':\n",
    "                ans.append('o')\n",
    "                idx += 2\n",
    "            else:\n",
    "                ans.append('al')\n",
    "                idx += 4\n",
    "        return ''.join(ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace('()','o').replace('(al)',\"al\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        # 思路：哈希表保存三个字符串及其对应的解析字符串\n",
    "\n",
    "        # paser_dict = {\"G\": \"G\", \"()\": \"o\", \"(al)\": \"al\"}\n",
    "        # ans = \"\"\n",
    "        # temp = \"\"\n",
    "        # for ch in command:\n",
    "        #     temp += ch\n",
    "        #     if temp in paser_dict:\n",
    "        #         ans += paser_dict[temp]\n",
    "        #         temp = \"\"\n",
    "        # return ans\n",
    "\n",
    "        res = []\n",
    "        for i, c in enumerate(command):\n",
    "            if c == 'G':\n",
    "                res.append(c)\n",
    "            elif c == '(':\n",
    "                res.append('o' if command[i + 1] == ')' else 'al')\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = [];\n",
    "        for i,c in enumerate(command):\n",
    "            if c=='G':\n",
    "                res.append(c)\n",
    "            elif c=='(':\n",
    "                res.append('o' if command[i+1]==')' else 'al')\n",
    "        return ''.join(res);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        i=0\n",
    "        ans=\"\"\n",
    "        while i<len(command):\n",
    "            if command[i]==\"G\":\n",
    "                ans+=\"G\"\n",
    "                i+=1\n",
    "            elif command[i:i+2]==\"()\":\n",
    "                ans+=\"o\"\n",
    "                i+=2\n",
    "            else:\n",
    "                ans+=\"al\"\n",
    "                i+=4\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res1 = command.replace(\"()\", \"o\")\n",
    "        res = res1.replace(\"(al)\", \"al\")\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace('()','o').replace('(al)','al')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re as r\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        x=''\n",
    "        for j,i in enumerate(command):\n",
    "            if i=='G':\n",
    "                x=x+'G'\n",
    "            if i=='(' :\n",
    "                if command[j+1]==')':\n",
    "                    x=x+'o'\n",
    "                else:\n",
    "                    pass\n",
    "            if i=='a':\n",
    "                x=x+'al'\n",
    "            else:\n",
    "                pass\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command = command.replace('()', 'o')\n",
    "        command = command.replace('(al)', 'al')\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\",\"o\").replace(\"(al)\",\"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = []\n",
    "        for i, c in enumerate(command):\n",
    "            if c == 'G':\n",
    "                res.append(c)\n",
    "            elif c == '(':\n",
    "                res.append('o' if command[i + 1] == ')' else 'al')\n",
    "        return ''.join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace('()', 'o').replace('(al)', 'al')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        ans = ''\n",
    "        pre = ''\n",
    "        for i in range(len(command)):\n",
    "            a = pre+command[i]\n",
    "            if a == 'G':\n",
    "                ans+='G'\n",
    "            elif a =='()':\n",
    "                ans+='o'\n",
    "                pre = ''\n",
    "            elif a == '(al)':\n",
    "                ans += 'al'\n",
    "                pre = ''\n",
    "            elif a == '/':\n",
    "                ans+='/'\n",
    "            else:\n",
    "                pre = a\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        ans=[]\n",
    "        a=0\n",
    "        for i in command:\n",
    "            if i=='G':\n",
    "                ans.append('G')\n",
    "            elif i=='(':\n",
    "                if command[a+1]==')':\n",
    "                    ans.append('o')\n",
    "                else:\n",
    "                    ans.append('al')\n",
    "            a+=1\n",
    "        \n",
    "        return ''.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        # 思路：哈希表保存三个字符串及其对应的解析字符串\n",
    "\n",
    "        paser_dict = {\"G\": \"G\", \"()\": \"o\", \"(al)\": \"al\"}\n",
    "        ans = \"\"\n",
    "        temp = \"\"\n",
    "        for ch in command:\n",
    "            temp += ch\n",
    "            if temp in paser_dict:\n",
    "                ans += paser_dict[temp]\n",
    "                temp = \"\"\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = []\n",
    "        for i, c in enumerate(command):\n",
    "            if c == 'G':\n",
    "                res.append(c)\n",
    "            elif c == '(':\n",
    "                res.append('o' if command[i + 1] == ')' else 'al')\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = []\n",
    "        i = 0\n",
    "        while i<len(command):\n",
    "            if command[i]=='G':\n",
    "                res.append('G')\n",
    "                i += 1\n",
    "            elif i+1<len(command) and command[i]=='(' and command[i+1]==')':\n",
    "                res.append('o')\n",
    "                i += 2\n",
    "            elif i+1<len(command) and command[i]=='(' and command[i+1]=='a':\n",
    "                res.append('al')\n",
    "                i += 4\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res = []\n",
    "        for i, c in enumerate(command):\n",
    "            if c == 'G':\n",
    "                res.append(c)\n",
    "            elif c == '(':\n",
    "                res.append('o' if command[i + 1] == ')' else 'al')\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        Hashtable ={'G':'G','()':'o','(al)':'al'}\n",
    "        res =[]\n",
    "        for i in range(len(command)):\n",
    "            if 'G' == command[i]:\n",
    "                res.append(Hashtable['G'])\n",
    "            elif '(' == command[i] and ')'==command[i+1]:\n",
    "                res.append(Hashtable['()'])\n",
    "            elif '(' == command[i] and 'a'==command[i+1]: \n",
    "                res.append(Hashtable['(al)'])\n",
    "        return ''.join(res)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        if len(command) == 1:\n",
    "            return command\n",
    "        transformStr = \"\"\n",
    "        i = 0\n",
    "        while i < len(command) - 1:\n",
    "            if command[i] == 'G':\n",
    "                transformStr += 'G'\n",
    "            elif command[i] == '(':\n",
    "                if command[i+1] == 'a':\n",
    "                    transformStr += 'al'\n",
    "                    i += 3\n",
    "                else:\n",
    "                    transformStr += 'o'\n",
    "                    i += 1\n",
    "            i += 1\n",
    "            if i == len(command) - 1 and command[i] == 'G':\n",
    "                transformStr += 'G'\n",
    "        return transformStr     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res=[]\n",
    "        for i,c in enumerate(command):\n",
    "            if c=='G':\n",
    "                res.append(c)\n",
    "            elif c=='(':\n",
    "                if command[i+1]==')':\n",
    "                    res.append('o')\n",
    "                else:\n",
    "                    res.append('al')\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace('()', 'o').replace('(al)', 'al')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        cmd_gen = (each for each in command)\n",
    "        res = ''\n",
    "        while True:\n",
    "            try:\n",
    "                p = next(cmd_gen)\n",
    "            except StopIteration:\n",
    "                return res\n",
    "            \n",
    "            if p == 'G':\n",
    "                res += 'G'\n",
    "                continue\n",
    "            \n",
    "            if p == '(':\n",
    "                pn = next(cmd_gen)\n",
    "                if pn == ')':\n",
    "                    res += 'o'\n",
    "                    continue\n",
    "                \n",
    "                while next(cmd_gen) != ')':\n",
    "                    pass\n",
    "                res += 'al'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        ans = \"\"\n",
    "        i = 0\n",
    "        while i < len(command):\n",
    "            if command[i] == 'G':\n",
    "                ans += 'G'\n",
    "                i += 1\n",
    "            else:\n",
    "                if command[i + 1] == ')':\n",
    "                    ans += 'o'\n",
    "                    i += 2\n",
    "                else:\n",
    "                    ans += \"al\"\n",
    "                    i += 4\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res=[]\n",
    "        for i ,element in enumerate(command):\n",
    "            if element=='G':\n",
    "                res.append('G')\n",
    "            elif element=='(':\n",
    "                res.append('o' if command[i+1]==')' else 'al')\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        step1=command.replace('()','o')\n",
    "        step2=step1.replace('(al)','al')\n",
    "        return step2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        ans=''\n",
    "        f=0\n",
    "        t=''\n",
    "        dic={'G':'G','()':'o','(al)':'al'}\n",
    "        for i in command:\n",
    "            if f==0 and i=='G':\n",
    "                ans+=i\n",
    "            elif f==0 and i=='(':\n",
    "                f=1\n",
    "                t+=i\n",
    "            elif f==1 and i!=')':\n",
    "                t+=i\n",
    "            elif f==1 and i==')':\n",
    "                t+=i\n",
    "                ans+=dic[t]\n",
    "                t=''\n",
    "                f=0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command = command.replace('()','o').replace('(','').replace(')','')\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\", \"o\").replace(\"(al)\", \"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        result = \"\"\n",
    "        ind = 0\n",
    "        while ind < len(command):\n",
    "            if command[ind] == \"G\":\n",
    "                result += \"G\"\n",
    "                ind += 1\n",
    "            elif command[ind] == \"(\" and command[ind + 1] == \")\":\n",
    "                result += \"o\"\n",
    "                ind += 2\n",
    "            else:\n",
    "                result += \"al\"\n",
    "                ind += 4\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        q=''\n",
    "        b=0\n",
    "        for i in command:\n",
    "            if command[b]==\"G\":\n",
    "                q+=\"G\"\n",
    "            elif command[b] == '(':\n",
    "                if command[b+1] == ')':\n",
    "                    q+='o'\n",
    "                else:\n",
    "                    q+='al'\n",
    "            b+=1\n",
    "        return q\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\", \"o\").replace(\"(al)\", \"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\", \"o\").replace(\"(al)\", \"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace('(al)', 'al').replace('()', 'o')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace(\"()\",\"o\").replace(\"(al)\",\"al\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        res=[]\n",
    "        for i,c in enumerate(command):\n",
    "            if c==\"G\":\n",
    "                res.append(c)\n",
    "            elif c==\"(\":\n",
    "                if command[i+1]==')':\n",
    "                    res.append(\"o\")\n",
    "                else:\n",
    "                    res.append(\"al\")\n",
    "                    \n",
    "        return  ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str: \n",
    "        a=[]\n",
    "        for c,i in enumerate(command):\n",
    "            if i==\"G\":\n",
    "                a.append(i)\n",
    "            elif i==\"(\":\n",
    "                a.append(\"o\"if command[c+1]==\")\"else\"al\")\n",
    "        return ''.join(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        return command.replace('()', 'o').replace('(al)', 'al')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        dic = {\"G\":\"G\",\"()\":\"o\",\"(al)\":\"al\"}\n",
    "        slow = 0\n",
    "        fast = 1\n",
    "        ans = \"\"\n",
    "        while fast <= len(command):\n",
    "            if command[slow:fast] in dic:\n",
    "                ans += dic[command[slow:fast]]\n",
    "                slow = fast\n",
    "                fast += 1\n",
    "            else:\n",
    "                fast += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        command = command.replace(\"()\", \"o\")\n",
    "        command = command.replace(\"(al)\", \"al\")\n",
    "        return command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def interpret(self, command: str) -> str:\n",
    "        update = list(command)\n",
    "        for i in range(len(update)-1):\n",
    "            if update[i] ==\"(\":\n",
    "                if update[i+1]==\")\":\n",
    "                    update[i] =\"o\"\n",
    "                    update[i+1]=\"\"\n",
    "                else:\n",
    "                    update[i] =\"\"\n",
    "            if update[i] ==\")\":\n",
    "                 update[i] =\"\"\n",
    "        if update[-1]==\")\":\n",
    "            update[-1] =\"\"\n",
    "        output = \"\".join(update)\n",
    "        return output\n",
    "        \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
